package org.yaac.server.egql.evaluator;

import static com.google.common.collect.Sets.newHashSet;

import java.util.Set;

import org.yaac.server.egql.EntityProperty;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;



/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class StringEvaluator extends Evaluator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final String val;
	
	/**
	 * @param rawText
	 */
	public StringEvaluator(String rawText) {
		this.setText(rawText);
		
		// remove single quote
		String withoutQuote = rawText.substring(1, rawText.length() - 1);
		
		// replace \' with '
		this.val = withoutQuote.replace("\\'", "'");
	}

	@Override
	public EvaluationResult evaluate(ProcessDataRecord record) {
		return new EvaluationResult(val).withTitle(this.getText());
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((val == null) ? 0 : val.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		StringEvaluator other = (StringEvaluator) obj;
		if (val == null) {
			if (other.val != null)
				return false;
		} else if (!val.equals(other.val))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "StringEvaluator [val=" + val + "]";
	}

	@Override
	public Set<AggregationEvaluator> aggregationChildren() {
		return newHashSet();
	}
	
	@Override
	public Set<EntityProperty> nonAggregationProperties() {
		return newHashSet();
	}

	@Override
	public void validate() throws EGQLException {
		
	}
}
